package com.linsen.core.log.listener;

import com.linsen.core.common.enums.LoginEndpointType;
import com.linsen.core.log.enums.LogStorageType;
import com.linsen.core.log.event.LogEvent;
import com.linsen.core.log.model.Log;
import com.linsen.core.log.props.LinsenLogProperties;
import com.linsen.core.rabbitmq.constant.QueueConstant;
import com.linsen.core.rabbitmq.service.RabbitMQService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

/**
 * 注解形式，异步监听事件
 *
 * @author zhaoyong
 * @date 2023/3/14
 */
@Slf4j
@RequiredArgsConstructor
public class LogListener {
    private final LinsenLogProperties logProperties;
    private final RabbitMQService rabbitMQService;

    @Async
    @Order
    @EventListener(LogEvent.class)
    public void handle(LogEvent event) {
        Log source = (Log) event.getSource();
        // 发送日志到RabbitMQ
        if (LogStorageType.RABBITMQ.equals(this.logProperties.getLogStorageType())) {
            //根据日志终端类型分发到不同的队列里
            LoginEndpointType endpointType = source.getEndpointType();
            switch (endpointType) {
                case SYSTEM:
                    this.rabbitMQService.sendMessage(QueueConstant.SYSTEM_LOG_DIRECT_QUEUE, source);
                    break;
                case APP:
                    break;
                default:
            }
        } else {
            log.info("本地日志:{}", source);
        }
    }

}
